package com.wthink.hzgj.busInfo.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.busInfo.BusInfo
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.util.HashUtil
import com.wthink.hzgj.util.Log
import com.wthink.hzgj.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class BusInfoViewmodel : ViewModel(){

    val businfo = MutableLiveData(
        BusInfo("","","","",""
            ,"","","","")
    )

    fun queryBusInfo(){
        viewModelScope.launch(Dispatchers.IO) {
            val busInfo = DevService.queryBusInfo()
            if (busInfo is Result.Success) {
                val data = busInfo.data
                val carNum = data.car_num
                val vdrModel = data.vdr_model
                val vdrVendorid = data.vdr_vendorid
                val vid = data.vid
                val simno = data.simno
                val carColor = data.car_color
                val provinceId = data.province_id
                val cityId = data.city_id
                val carVin = data.car_vin
                
                businfo.postValue(data)
            }else if (busInfo is Result.Error) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, busInfo.exception.toString())
                }
            }
        }
    }
}